<?php


namespace app\model;


use think\facade\Db;

class AdminModel
{
    private static $_baseTable = 'eb_admin';

    /**检测管理员状态
     * @param $token
     * @return \think\response\Json
     */
    public static function checkStatus($token, $type = 0)
    {
        try {
            $redis = new \Redis();
            $redisConf = config('tools.redis');
            $redis->connect($redisConf['host'], $redisConf['port']);
            $adminId = $redis->get($token);
            if (!$adminId) return jsonFail('登录过期');
            // 获取状态
            $info = Db::table(self::$_baseTable)->field('status,type')->where('id', $adminId)->find();
            if ($info == null) return jsonFail('未找到管理员信息');
            if ($info['status'] != 1) return jsonFail('当前管理员不可用');
            if ($type != 0 && $info['type'] != $type) return jsonFail('当前用户没有权限');
            return jsonSuccess('');
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }


    /**
     * @param string $token
     * @param array $status
     * @param array $type
     * @return array|\think\Model|\think\response\Json|null
     */
    public static function getInfo($token,$status = [], $type = [])
    {
        try {
            $redis = new \Redis();
            $redisConf = config('tools.redis');
            $redis->connect($redisConf['host'], $redisConf['port']);
            $adminId = $redis->get($token);
            if (!$adminId) return jsonFail('请重新登录');
            // 获取状态
            $info = Db::table(self::$_baseTable)->field('id,status,type,pid')->where('id', $adminId)->find();
            if ($info == null) return jsonFail('获取管理员信息失败');
            if ($status!= [] && !in_array($info['status'], $status)) return jsonFail('当前无权操作');
            if ($type!= [] && !in_array($info['type'], $type)) return jsonFail('当前无权操作');
            return $info;
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
}